package com.jwsoft.manager.core.util;

import java.security.KeyFactory;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import cn.hutool.core.codec.Base64;
import com.alibaba.fastjson.JSON;
import com.jwsoft.manager.common.vo.common.XzjhPCUserVO;
import io.jsonwebtoken.*;
import org.apache.commons.lang3.StringUtils;


public class XzjhJWTUtil {
    private static Pattern cellphonePattern =
            Pattern.compile(
                    "^1(3[0-9]|4[0-9]|5[0-9]|6[0-9]|7[0-9]|8[0-9]|9[0-9])\\d{8}$"); // 1开头
    public static PrivateKey generatePrivateKey(String base64PrivateKey) throws Exception {
        byte[] privateKeyBytes = Base64.decode(base64PrivateKey);
        PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(privateKeyBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePrivate(spec);
    }


    public static PublicKey generatePublicKey(String base64PublicKey) throws Exception {
        byte[] publicBytes = Base64.decode(base64PublicKey);
        X509EncodedKeySpec spec = new X509EncodedKeySpec(publicBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        return keyFactory.generatePublic(spec);
    }

    public static XzjhPCUserVO getUser(String jwt, String baseKey) throws Exception {
        PublicKey publicKey = generatePublicKey(baseKey);
        Jwt<JwsHeader, Claims> t = Jwts.parserBuilder()
                .setSigningKey(publicKey)
                //.deserializeJsonWith(new CustomJwtDeserializer<>())
                .build()
                .parseClaimsJws(jwt);
        String userId = t.getBody().get("user_id").toString();
        String realName ="";
        if(t.getBody().get("realName")!=null){
            realName =t.getBody().get("realName").toString();
        }

        String email = "";
        if(t.getBody().get("email")!=null){
            email =t.getBody().get("email").toString();
        }
        String userName = "";
        if(t.getBody().get("name")!=null){
            userName =t.getBody().get("name").toString();
        }
        String mobile = "";
        if(t.getBody().get("mobile")!=null){
            mobile =t.getBody().get("mobile").toString();
        }
        if(StringUtils.isBlank(mobile)){
            Matcher m = cellphonePattern.matcher(userName);
            if (m.matches()) {
                mobile=userName;
            }
        }
        XzjhPCUserVO xzjhPCUserVO=new XzjhPCUserVO();
        xzjhPCUserVO.setUserId(userId);
        xzjhPCUserVO.setCellphone(mobile);
        xzjhPCUserVO.setEmail(email);
        xzjhPCUserVO.setUserName(userName);
        xzjhPCUserVO.setFullName(realName);
        return xzjhPCUserVO;
    }

    public static void main(String [] args) throws Exception {
        XzjhPCUserVO vo=getUser("eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJhZG1pbiIsImtpZCI6Ijk2ODU2OTM3Mzk5ODQ0ODY0MF9zaWciLCJtb2JpbGUiOiIxODY2ODIwNzMzMiIsImV4dGVybmFsX2lkIjoiMSIsImxvY2FsZSI6ImRlIiwib25saW5lX3RpY2tldCI6Ik9UOTY4NTgyNzkwMzY4OTg1MDg4IiwiYXVkIjoiOTY4NTY5MzczOTk4NDQ4NjQwIiwiaW5zdGl0dXRpb24iOiIxIiwidXNlcl9pZCI6IjEiLCJuYW1lIjoiYWRtaW4iLCJleHAiOjE3MTEwOTY4OTEsImlhdCI6MTcxMTA5NjU5MSwianRpIjoiYjA5ZDQ1YjktNzY5YS00YWUyLWI2YTktODg1OTI3MTE3YjY2IiwiZW1haWwiOiJhZG1pbkBReHp5QzgySldBLW1haWwuY29tIn0.CZYVzgb8bDpGceb3XgvSxmOJrFwWoaWCTCIbuGUpCuI1TDwb-eTj37znl7cdxacz8vqjE63ibAkOG4fYhb-_k2iWPE16u6z1G2Ob9KJfx3yhBWC3ZZEbF8XM-IA6eNfxrFaXvT1Vx4zIzuVsEYhoNj817NLtzMaYoPv8_3jtx9ag13pAtV9IeAEq3qW9XMl5lwshUegObvDfuKBJx8QAj80qseXD31v_27BakljBqFZyBezTnnoULnt7NHMBJ6LRtsg1JGrNk_6CFPcY0OEslOI0ZuYZ7-rZZS91XWX7ydVl7nNLF7Ung194BM3XpOhYWIsm92kKgonU16b330kMrQ","");
        System.out.println(JSON.toJSONString(vo));
    }
}
